﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Entities;
using DAL;
using System.Data.SqlClient;
using System.Configuration;
using BLToolkit.Data;

namespace CuaHangBangDia.DoanhThu
{
    public partial class CapNhatNhapHangForm : Form
    {
        
        int item = 12;
        int page = 1;
        int tong;       
        bool _selectAll = false;
        public CapNhatNhapHangForm()
        {
            InitializeComponent();
        }

        private void frmDoanhThu_Load(object sender, EventArgs e)
        {           
            
            LoadNhapHang(getPage(page));

        }

       
      
        private void LoadNhapHang(List<NhapHang> ds)
        {
            dtLoad.DataSource = ds;            
            tong = TongTrang(NhapHang.Objects.GetAll().ToList().Count, item);
            txtTrang.Text = Convert.ToString(page);
            lbTongTrang.Text = "/ " + tong;

        }

        public List<NhapHang> getPage(int page)
        {
            dtLoad.Columns[1].DefaultCellStyle.Format = "dd/MM/yyyy";
            var query = from c in NhapHang.Objects.GetAll().ToList()
                           .Skip(item * page - item).Take(item)
                        select c;
            return query.ToList();

        }
        public static int TongTrang(int soluongdl, int num)
        {
            int kq = (int)(soluongdl / num);
            if (kq * num == soluongdl)
                return kq;
            return kq + 1;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {

            if (page < tong)
                page++;
            if (page <= tong)
            {
                txtTrang.Text = Convert.ToString(page);
                LoadNhapHang(getPage(page));
            }

        }



        private void btnBack_Click(object sender, EventArgs e)
        {

            if (page >= 2)
            {
                page--;
                txtTrang.Text = Convert.ToString(page);
                LoadNhapHang(getPage(page));
            }



        }

     



        private void dtLoad_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }
        private NhapHang Getdata(int i)
        {
            NhapHang s = new NhapHang();            
            s.NgayNhap = Convert.ToDateTime(dtLoad.Rows[i].Cells[3].Value.ToString());
            s.SoLuong = Convert.ToInt32(dtLoad.Rows[i].Cells[4].Value.ToString());
            s.TongTien = Convert.ToDouble(dtLoad.Rows[i].Cells[5].Value.ToString());            
            return s;
        }

        private void dtLoad_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int n = dtLoad.Rows.Count;
            if (e.ColumnIndex == 0 && n > 0)
            {
                _selectAll = !_selectAll;
                for (int i = 0; i < n; i++)
                {
                    dtLoad.Rows[i].Selected = _selectAll;
                    dtLoad.Rows[i].Cells[0].Value = _selectAll;
                }


            }
        }

        private void btnTK_Click(object sender, EventArgs e)
        {
            
            if (rbNgay.Checked)
            {
                TimTheoNgay(dtpNgayNhap.Value);
            }
            else if (rbThang.Checked)
            {
                TimTheoThang(dtpNgayNhap.Value);
            }
            else if (rbNam.Checked)
            {
                TimTheoNam(dtpNgayNhap.Value);
            }
            else
            {
                MessageBox.Show("Chọn Kiểu Thông Báo", "Thông Báo");
            }
                
        }

        private void TimTheoNgay(DateTime dt)
        {
            List<NhapHang> kq = NhapHang.Objects.Find(b =>b.NgayNhap.Equals(dt.ToString("MM/dd/yyyy"))).ToList();
            if (kq.Count > 0)
            {
                LoadNhapHang(kq);
            }
            else
            {
                dtLoad.DataSource = null;
                MessageBox.Show("Không Tìm Thấy", "Thông Báo");

            }
        }
        private void TimTheoThang(DateTime dt)
        {
            List<NhapHang> kq = NhapHang.Objects.Find(b => b.NgayNhap.Year==(dt.Year) && b.NgayNhap.Month==(dt.Month)).ToList();
            if (kq.Count > 0)
            {
                LoadNhapHang(kq);
            }
            else
            {
                dtLoad.DataSource = null;
                MessageBox.Show("Không Tìm Thấy", "Thông Báo");

            }
        }
        private void TimTheoNam(DateTime dt)
        {
            List<NhapHang> kq = NhapHang.Objects.Find(b => b.NgayNhap.Year==dt.Year).ToList();
            if (kq.Count > 0)
            {
                LoadNhapHang(kq);
            }
            else
            {
                dtLoad.DataSource = null;
                MessageBox.Show("Không Tìm Thấy", "Thông Báo");

            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {

            this.Close();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            dtLoad.ClearSelection();
            LoadNhapHang(getPage(page));
        }

        

        private void dtLoad_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string ten = dtLoad.Columns[e.ColumnIndex].Name;           
            NhapHang x = new NhapHang();
            NhapHang s = new NhapHang();

            if (ten == "btnXoa")
            {

                for (int i = 0; i < dtLoad.Rows.Count; i++)
                {
                    if (dtLoad.Rows[i].Cells[0].Value != null)
                    {
                        NhapHang t = Getdata(i);
                        if (NhapHang.Objects.Delete(t))
                        {
                            MessageBox.Show("Xóa Thành Công", "Thông Báo");
                            btnLoad.PerformClick();
                        }
                    }
                }
            }
            else if (ten == "btnSua")
            {
                for (int i = 0; i < dtLoad.Rows.Count; i++)
                {
                    if (dtLoad.Rows[i].Cells[0].Value != null)
                    {
                        s = Getdata(i);                        
                        if (NhapHang.Objects.Update(s) == true)
                        {

                            MessageBox.Show("Sửa Thành Công", "Thông Báo");
                            btnLoad.PerformClick();
                        }
                        else
                        {
                        }
                    }

                }
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (dtLoad.Rows.Count > 0)
            {
                for (int i = 0; i < dtLoad.Rows.Count; i++)
                {

                    dtLoad.Rows[i].Cells[0].Value = null;

                }
            }
        }

        private void txtTrang_KeyDown(object sender, KeyEventArgs e)
        {
            int t;
            if (e.KeyData == Keys.Enter)
            {
                try
                {
                    t = Convert.ToInt32(txtTrang.Text);
                    if (t <= tong && t > 0)
                    {
                        LoadNhapHang(getPage(t));
                        txtTrang.Text = Convert.ToString(t);
                        page = t;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Nhâp Trang Muốn Đến");
                }

            }
        }
       

       

        

        

    }
}

